package ce.com.newbluedemo.k2;

import android.annotation.TargetApi;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.cenewbluesdk.uitl.TimeUtil;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/28.
 */

public class ActivitySendMsg extends K2BaseActivity implements View.OnFocusChangeListener, View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private EditText mEtName;
    private EditText mEtContent;
    private Button mBtnSend;
    private final int TYPE_NUM = 0;
    private final int TYUPE_NAME = 1;
    private final int TYPE_CONTENT = 2;
    private String mName;
    private String mContent;
    private int mType = 0;

    private CEDevK2Proxy k2Proxy;

    private Handler mK2Handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0){
                showToast("发送失败，请稍后重试！");
            } else {
                mEtName.setText("");
                mEtContent.setText("");
                showToast("发送成功！");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_msg);
        k2Proxy = BleFactory.getInstance().getK2Proxy();
        k2Proxy.setHandler(mK2Handler);
        mEtName = (EditText) findViewById(R.id.et_name);
        mEtContent = (EditText) findViewById(R.id.et_content);
        mBtnSend = (Button) findViewById(R.id.btn_send);
        mEtName.setOnFocusChangeListener(this);
        mEtContent.setOnFocusChangeListener(this);
        mBtnSend.setOnClickListener(this);
        ((RadioGroup) findViewById(R.id.rg_msg_type)).setOnCheckedChangeListener(this);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) return;
        switch (v.getId()) {
            case R.id.et_name:
                if (mEtName.getText().toString().isEmpty()) {
                    showToast("invalid name input");
                } else {
                    mName = mEtName.getText().toString();
                }
                break;
            case R.id.et_content:
                if (mEtContent.getText().toString().isEmpty()) {
                    showToast("invalid content input");
                } else {
                    mContent = mEtContent.getText().toString();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if (!k2Proxy.isConnectOK()) {
            Toast.makeText(this, "蓝牙已经断开", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mName) || TextUtils.isEmpty(mContent)) {
            showToast("输入有误，请检查输入后重试");
            return;
        }
        switch (v.getId()) {
            case R.id.btn_send:
                k2Proxy.getSendHelper().
                        sendMessage_notice(TimeUtil.now(), mName, mContent, (byte) mType);
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.rg_msg_type:
                switch (checkedId) {
                    case R.id.rb_num:
                        mType = TYPE_NUM;
                        break;
                    case R.id.rb_name:
                        mType = TYUPE_NAME;
                        break;
                    case R.id.rb_content:
                        mType = TYPE_CONTENT;
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }
}
